package com.mm.learn.juc;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

/**
 * @author wdx
 * @date 2021年08月09日 11:10
 * 公平非公平锁
 * 可重入锁（递归锁）
 * 自旋锁
 * 读（共享）锁  写（独占）锁
 */
@Slf4j
public class LockTest {

    static Object object = new Object();
    static boolean t2run = false;

    /*public static void main(String[] args) {
        testLock();
    }*/

    //死锁
    private static void testLock() {
        Object a = new Object();
        Object b = new Object();
        Thread thread = new Thread(() -> {
            synchronized (a) {
                System.out.println("lock a");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (b) {
                    System.out.println("lock b");
                    System.out.println("操作");
                }
            }
        }, "t1");

        Thread thread1 = new Thread(() -> {
            synchronized (b) {
                System.out.println("lock b");
                try {
                    TimeUnit.MILLISECONDS.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (a) {
                    System.out.println("lock a");
                    System.out.println("操作");
                }
            }
        }, "t2");

        thread.start();
        thread1.start();
    }


    //同步顺序执行  wait notify
    /*public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            synchronized (object) {
                while (!t2run) {
                    try {
                        object.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                log.debug("1");
            }
        }, "t1");

        Thread t2 = new Thread(() -> {
            synchronized (object) {
                log.debug("2");
                t2run = true;
                object.notify();
            }
        }, "t2");

        t1.start();
        t2.start();
    }*/

    /*//同步顺序执行  park unpark
    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            LockSupport.park();
            log.debug("1");
        }, "t1");

        t1.start();

        Thread t2 = new Thread(() -> {
            log.debug("2");
            LockSupport.unpark(t1);
        }, "t2");

        t2.start();
    }*/

    /**
     * 输出内容     等待标记   下一个标记
     * a            1           2
     * b            2           3
     * c            3           1
     */


    /*public static void main(String[] args) {
        WaitNotify waitNotify = new WaitNotify(5,1);
        new Thread(()->{
            waitNotify.print("a",1,2);
        },"t1").start();
        new Thread(()->{
            waitNotify.print("b",2,3);
        },"t2").start();
        new Thread(()->{
            waitNotify.print("c",3,1);
        },"t3").start();
    }


}

//交替输出
class WaitNotify {

    //循环次数
    private int loopNumber;
    //等待标记
    private int flag;

    public WaitNotify(int loopNumber, int flag) {
        this.loopNumber = loopNumber;
        this.flag = flag;
    }

    //打印                    a          1              2
    public void print(String str, int waitFlag, int nexFlag) {
        for (int i = 0; i < loopNumber; i++) {
            synchronized (this) {
                while (flag != waitFlag) {
                    try {
                        this.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.print(str);
                flag = nexFlag;
                this.notifyAll();
            }
        }
    }*/

  /*  //Await Signal
    public static void main(String[] args) {
        AwaitSignal awaitSignal = new AwaitSignal(5);
        Condition a = awaitSignal.newCondition();
        Condition b = awaitSignal.newCondition();
        Condition c = awaitSignal.newCondition();
        new Thread(()->{
            awaitSignal.print("a",a,b);
        },"t1").start();
        new Thread(()->{
            awaitSignal.print("b",b,c);
        },"t2").start();
        new Thread(()->{
            awaitSignal.print("c",c,a);
        },"t3").start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try {
            awaitSignal.lock();
            a.signal();
        }finally {
            awaitSignal.unlock();
        }
    }*/
    static Thread t1;
    static Thread t2;
    static Thread t3;
    public static void main(String[] args) {
        ParkUnpark parkUnpark = new ParkUnpark(5);
        t1 = new Thread(()->{
            parkUnpark.print("a",t2);
        },"t1");
        t2 = new Thread(()->{
            parkUnpark.print("b",t3);
        },"t2");
        t3 = new Thread(()->{
            parkUnpark.print("c",t1);
        },"t3");
        t1.start();
        t2.start();
        t3.start();
        LockSupport.unpark(t1);
    }


}

/*class AwaitSignal extends ReentrantLock {
    private int loopNumber;

    public AwaitSignal(int loopNumber) {
        this.loopNumber = loopNumber;
    }

    //打印                  打印内容         进入哪一间休息室      下一间休息室
    public void print(String str,Condition current,Condition next){
        for (int i = 0; i < loopNumber; i++) {
            this.lock();
            try {
                current.await();
                System.out.print(str);
                next.signal();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                unlock();
            }
        }

    }

}*/




class ParkUnpark{
    private int loopNumber;

    public ParkUnpark(int loopNumber) {
        this.loopNumber = loopNumber;
    }
    public void print(String str,Thread next){
        for (int i = 0; i < loopNumber; i++) {
            LockSupport.park();
            System.out.print(str);
            LockSupport.unpark(next);

        }
    }
}

